#pragma once

#include <iostream>
#include <cstring>
#include <cerrno>
#include <string>
#include <pthread.h>
#include <functional>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>

#include "Log.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"
#include "ThreadPool.hpp"

#define BACKLOG 8

using namespace LogModule;
using namespace ThreadPoolModule;

static const uint16_t gport = 8080;
using handler_t = std::function<std::string(std::string&)>;

class TcpServer
{
    using task_t = std::function<void()>;
    struct ThreadData
    {
        int sockfd;
        TcpServer *self;
    };

public:
    TcpServer(handler_t handler, uint16_t port = gport)
        :_handler(handler),
        _port(port)
    {}

    void InitServer()
    {
        // 1.创建tcp socket
        _listensockfd = ::socket(AF_INET,SOCK_STREAM, 0); //Tcp Socket
        if(_listensockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket error";
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket create success, sockfd is: " << _listensockfd;

        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY;

        // 2. bind
        int n = ::bind(_listensockfd, CONV(&local), sizeof(local));
        if(n < 0)
        {
            LOG(LogLevel::FATAL) << "bind error: " << strerror(errno);
            Die(ExitError::BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success, sockfd is: " << _listensockfd;

        // 3.cs模式，tcp面向连接的，要求tcp随时对滴等待被连接
        // tcp 需要将socket设置为监听状态
        n = ::listen(_listensockfd, BACKLOG);
        if(n < 0)
        {
            LOG(LogLevel::FATAL) << "listen error: " << strerror(errno) << " sockfd is: " << _listensockfd;
            Die(ExitError::LISTEN_ERR);
        }
        LOG(LogLevel::INFO) << "listen success, sockfd is: " << _listensockfd;

        //::signal(SIGCHLD, SIG_IGN);  // 子进程退出，OS会自动回收资源，不再wait了
    }

    void HandleRequest(uint16_t sockfd) //TCP也是全双工的
    {
        LOG(LogLevel::INFO) << "HandleRequest, sockfd is: " << sockfd;
        char inbuffer[4096];
        std::string package;

        //长任务
        while(true)
        {
            // 约定：用户发过来的是一个完整的命令string
            //ssize_t n = ::read(sockfd, inbuffer, sizeof(inbuffer) - 1); // read读取时不完善的
            ssize_t n = ::recv(sockfd, inbuffer, sizeof(inbuffer) - 1, 0); 
            if(n > 0)
            {
                inbuffer[n] = 0;
                package += inbuffer;
                LOG(LogLevel::INFO) << "\n" << inbuffer;
                
                // std::string echo_str = "server echo# ";
                // echo_str += inbuffer;

                std::string cmd_result = _handler(package); // 调用用户处理函数
                if(cmd_result.empty()) continue; //TODO

                ::send(sockfd, cmd_result.c_str(), cmd_result.size(), 0);
            }
            else if(n == 0)
            {
                // read 如果读取返回值是0，表示client退出
                LOG(LogLevel::INFO) << "client quit, sockfd: " << sockfd;
                break;
            }
            else
            {
                // 读取失败
                break;
            }
        }
        ::close(sockfd); //fd泄漏问题
    }

    static void* ThreadEntry(void* args)
    {
        pthread_detach(pthread_self()); // 线程分离，不再受到父进程的控制
        ThreadData *data = (ThreadData*)args;
        data->self->HandleRequest(data->sockfd);
        return nullptr;
    }

    void Start()
    {
        _isrunning = true;
        while(_isrunning)
        {
            //不能直接读取数据
            // 1.获取新连接
            struct sockaddr_in peer;
            socklen_t peerlen = sizeof(peer); // 这个地方一定要注意初始化peerlen，要不然会出现问题！！！
            LOG(LogLevel::INFO) << "waiting for connection...";
            // 我们要获取client的信息：数据(sockfd)+client socket信息（accep||recvfrom）
            int sockfd = ::accept(_listensockfd, CONV(&peer), &peerlen);
            if(sockfd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error: " << strerror(errno); 
                continue;
            }

            // 获取连接成功
            LOG(LogLevel::INFO) << "accept success, sockfd is: " << sockfd;
            InetAddr addr(peer);
            LOG(LogLevel::INFO) << "client info: " << addr.Addr();

            // version-0
            //HandleRequest(sockfd);

            // version-1: 多进程版本
            // pid_t id = fork();
            // if(id == 0)
            // {
            //     // child process
            //     // 问题1：子进程继承父进程的文件描述符表，有两张，父子各一张
            //     // 1.关闭不需要的fd
            //     ::close(_listensockfd);

            //     // 孙子进程 -> 孤儿进程 -> 僵尸进程,由pid为1的进程管理
            //     if(fork() > 0) exit(0); // 子进程退出，父进不会wait，继续运行

            //     HandleRequest(sockfd);
            //     exit(0);
            // }

            // ::close(sockfd); //fd泄漏问题
            // //不会阻塞
            // int rid = ::waitpid(id, nullptr, 0);
            // if(rid < 0)
            // {
            //     LOG(LogLevel::WARNING) << "waitpid error: " << strerror(errno);
            // }

            // version-2: 多线程版本
            // pthread_t tid;
            // ThreadData *data = new ThreadData;
            // data->sockfd = sockfd;
            // data->self = this;
            // pthread_create(&tid, nullptr, ThreadEntry, data); //主线程和新线程是如何看待文件描述符表的？

            // version-3: 线程池版本 比较适合处理短任务，或者用户量少的情况
            //task_t f = std::bind(&TcpServer::HandleRequest, this, sockfd);  //构建任务
            ThreadPool<task_t>::getINstance()->Equeue([this, sockfd](){
                this->HandleRequest(sockfd);
            }); //入队
        }
    }

    void Stop()
    {
        _isrunning = false;
    }

    ~TcpServer(){}

private:
    int _listensockfd;  //监听套接字
    uint16_t _port;
    bool _isrunning;

    // 处理上层任务的入口
    handler_t _handler;
};